// Prevents additional console window on Windows in release, DO NOT REMOVE!!
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]

use data::category_data::{category_update_sort, category_insert, category_search, category_update, category_delete};
use data::goods_data::{goods_search, goods_search_all, goods_insert, goods_update, goods_delete};
use crate::data::goods_data::{get_goods_money, goods_result_all, result_all};
use crate::data::order_data::{search_order_by_goods, order_insert, order_serach_all, order_search_by_condiction,  order_find_by_goods_id, order_del_by_id};
use crate::data::repo_data::{repo_insert, repo_delete, repo_change};
use crate::data::choose_data::{choose_search, choose_update};
use crate::domin::{Goods, Repo};

mod data;
mod domin;


#[tauri::command]
fn goods_change(goods: Goods) -> usize {
  goods_update(goods).expect("修改商品出错")
}

#[tauri::command]
fn goods_del(id: String) -> usize {
  goods_delete(id).expect("删除商品出错")
}

#[tauri::command]
fn goods_searc(goods: Goods) -> String {
  goods_search(goods).expect("查询商品表出错")
}

#[tauri::command]
fn goods_seac_all() -> String {
  goods_search_all().expect("查询不到数据")
}

#[tauri::command]
fn order_search_all() -> String {
  order_serach_all().expect("查询订单错误")
}

#[tauri::command]
fn goods_money() -> String {
  get_goods_money().expect("获取总额错误")
}

#[tauri::command]
fn order_search(start_time: String, end_time: String) -> String {
  order_search_by_condiction(start_time, end_time).expect("获取订单错误")
}

#[tauri::command]
fn order_find(id: String) -> String {
  order_find_by_goods_id(id).expect("获取订单错误")
}

#[tauri::command]
fn repo_add(repo: Repo) -> usize {
  repo_insert(repo).expect("添加库存失败")
}

#[tauri::command]
fn repo_del(id: String) -> usize {
  repo_delete(id).expect("删除库存失败")
}

#[tauri::command]
fn get_result_all() -> String {
  result_all().expect("查询综合体失败")
}

#[tauri::command]
fn get_goods_result_all() -> String {
  goods_result_all().expect("get_goods_result_all expect : ")
}


fn main() {
  // 如果没有数据库表, 则创建数据库表
  let _ = data::create_data::create_category();
  let _ = data::create_data::create_goods();
  let _ = data::create_data::create_repo();
  let _ = data::create_data::create_order();
  let _ = data::create_file();
  let _ = data::create_data::create_category_choose();


  tauri::Builder::default()
    .invoke_handler(tauri::generate_handler![
            category_insert,
            category_search,
            category_update,
            category_delete,
            category_update_sort,
            goods_insert,
            goods_searc,
            goods_seac_all,
            goods_change,
            goods_del,
            order_search_all,
            goods_money,
            order_search,
            order_find,
            order_insert,
            repo_add,
            get_result_all,
            repo_del,
            get_goods_result_all,
            search_order_by_goods,
            repo_change,
            order_del_by_id,
            choose_search,
            choose_update
        ])
    .plugin(tauri_plugin_window_state::Builder::default().build())  // 添加这一行代码
    .run(tauri::generate_context!())
    .expect("error while running tauri application");
}
